<template>
  <div>

    <!-- 注册表单 -->
    <van-form @submit="onSubmit"
      ref="loginForm">
      <!-- 账号 -->
      <van-field v-model="user.mobile"
        name="mobile"
        label="账号"
        placeholder="请输入手机号"
        type="number"
        maxlength="11"
        clearable
        :rules="userFormRules.mobile" />
        <!-- 密码 -->
      <van-field v-model="user.password"
        type="text"
        name="password"
        maxlength="11"
        label="密码"
        placeholder="请输入密码"
        :rules="userFormRules.password" />

        <!-- 昵称 -->
        <van-field v-model="user.nickname"
        type="text"
        name="nickname"
        maxlength="11"
        label="昵称"
        placeholder="请输入昵称"
        :rules="userFormRules.nickname"/>
        <!-- 验证码 -->
      <van-field type="number"
        maxlength="6"
        name="code"
        label="验证码"
        placeholder="请输入验证码"
        v-model="user.code"
        :rules="userFormRules.code">
        <template #button>
          <!-- 倒计时 -->
          <van-count-down millisecond
            :time="1000*5"
            format="ss s"
            v-if="isCountDownShow"
            @finish="isCountDownShow=false" />
          <van-button class="send-sms-btn"
            v-else
            round
            size="small"
            native-type="button"
            @click="onSendSms"
            type="default">获取验证码</van-button>
        </template>
      </van-field>

      <div style="margin: 16px;">
        <van-button round
          block
          type="info"
          native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>

export default {
  data () {
    return {
      name: 'Register',
      // 表单数据
      user: {
        mobile: '', // 手机号
        password: '', // 密码,
        nickname: '', // 昵称
        code: '' // 验证码
      },
      // 表单验证规则
      userFormRules: {
        mobile: [{ required: true, message: '手机号不能为空' }, {
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '手机号格式错误'
        }],

        password: [{ required: true, message: '密码不能为空' }, {
          pattern: /^[a-zA-Z]\w{5,17}$/,
          message: '密码格式错误'
        }],

        nickname: [{ required: true, message: '昵称不能为空' }, {
          pattern: /[^~\x22]+/,
          message: '昵称格式错误'
        }],
        code: [{
          required: true,
          message: '验证码不能为空'
        }, {
          pattern: /^\d{4}$/,
          message: '验证码格式错误'
        }]
      },
      // 控制倒计时展示开关
      isCountDownShow: false

    }
  },

  methods: {

    // 提交表单
    async  onSubmit () {
      // console.log('submit', values)
      // loading 转圈圈提示
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0 // 持续展示 toast
      })
      // 验证验证码
      try {
        const { data: res } = await this.$http.get('/login/cellphone', {
          params: {
            captcha: this.user.code,
            phone: this.user.mobile,
            password: this.user.password,
            nickname: this.user.nickname
          }
        })
        console.log(res)
        this.$store.commit('setUser', res)
        this.$toast.success('登录成功')
        // 登录成功，跳转到原来的页面
        this.$router.push('/my')
      } catch (err) {
        console.log(err.response)
        if (err.response.status === 400) {
          this.$toast.fail('验证码错误')
        } else if (err.response.status === 501) {
          this.$toast.fail('账号不存在')
        }
      }
    },
    // 发送验证码
    async  onSendSms () {
      console.log('验证码')
      // 1. 校验手机号
      try {
        await this.$refs.loginForm.validate('mobile')
        console.log(this.$refs.loginForm)
      } catch (err) {
        return console.log('验证失败', err)
      }
      // 2. 验证通过，显示倒计时
      this.isCountDownShow = true
      // 3. 请求发送验证码
      const data = await this.$http.get('/captcha/sent', {
        params: {
          phone: this.user.mobile
        }
      })
      console.log(data)
    }
  }
}
</script>

<style>

</style>
